package com.micangcaihang.micang.mvp.login.presenter;

import com.micangcaihang.common.http.ApiResult;
import com.micangcaihang.common.http.exception.ApiException;
import com.micangcaihang.micang.entity.UserEntity;
import com.micangcaihang.micang.http.MiSubScriber;
import com.micangcaihang.micang.mvp.login.contract.LoginContract;
import com.micangcaihang.micang.mvp.login.model.LoginModel;

import io.reactivex.Observable;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/7/30
 *     update : 2017/7/30
 *     desc   : 登录
 *     version: 1.0
 * </pre>
 */

public class LoginPresenter extends LoginContract.Presenter {
    private LoginModel mLoginModel;
    private String username;
    private String password;

    @Override
    public void setV(LoginContract.View v) {
        super.setV(v);
        mLoginModel = new LoginModel();
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 每次进入界面获取网络数据，更新界面
     *
     * @param isRefresh 新增参数，用来判断是否为下拉刷新调用，下拉刷新的时候不应该再显示加载界面和异常界面
     */
    @Override
    public void loadFirstData(boolean isRefresh) {

    }

    /**
     * 加载更多数据
     */
    @Override
    public void loadMoreData() {

    }

    /**
     * 登录
     */
    @Override
    public void httpLogin(String username, String password) {
        Observable<ApiResult<UserEntity>> userData = mLoginModel.getUserData(username, password);

        mRxManage.add(userData.subscribeWith(new MiSubScriber<ApiResult<UserEntity>>() {

            @Override
            protected void doOnStart() {
                mView.showDialogLoading(null);
            }

            @Override
            protected void doOnNext(ApiResult<UserEntity> userEntityApiResult) {
                mView.returnLoginData(userEntityApiResult.data);
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }
        }));
    }

}
